//package com.ajz.config.jwt;
//
//import com.ajz.security.entity.User;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import io.jsonwebtoken.Jwts;
//import io.jsonwebtoken.SignatureAlgorithm;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
//
//import javax.inject.Inject;
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.Date;
//
///**
// * 
// * <p>
// * JWTLoginFilter，核心功能是在验证用户名密码正确后，生成一个token，并将token返回给客户端：
// * </p>
// *
// * @author xyj
// * @date 2018年11月1日
// */
//public class JWTLoginFilter extends UsernamePasswordAuthenticationFilter {
//	@Inject
//	private AuthenticationManager authenticationManager;
//
//	/**
//	 * 接收并解析用户凭证
//	 */
//	@Override
//	public Authentication attemptAuthentication(HttpServletRequest req, HttpServletResponse res)
//			throws AuthenticationException {
//		try {
//			User user = new ObjectMapper().readValue(req.getInputStream(), User.class);
//
//			return authenticationManager.authenticate(
//					new UsernamePasswordAuthenticationToken(user.getLoginName(), user.getPassword(), new ArrayList<>()));
//		} catch (IOException e) {
//			throw new RuntimeException(e);
//		}
//	}
//
//	/**
//	 * 用户成功登录后，这个方法会被调用，我们在这个方法里生成token
//	 */
//	@Override
//	protected void successfulAuthentication(HttpServletRequest req, HttpServletResponse res, FilterChain chain,
//			Authentication auth) throws IOException, ServletException {
//
//		String token = Jwts.builder().setSubject(((User) auth.getPrincipal()).getLoginName())
//				.setExpiration(new Date(System.currentTimeMillis() + 60 * 60 * 24 * 1000))
//				.signWith(SignatureAlgorithm.HS512, "secretkey").compact();
//		res.addHeader("Authorization", "Bearer " + token);
//	}
//}
